package com.iotimc.attendancesystem.manage.person;


import android.content.Context;
import android.content.DialogInterface;
import android.support.v7.app.AlertDialog;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CheckBox;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import android.widget.TextView;

import com.iotimc.attendancesystem.R;
import com.iotimc.attendancesystem.entity.PageHelper;
import com.iotimc.attendancesystem.entity.PersonEntity;
import com.iotimc.attendancesystem.util.CustomDialog;
import com.iotimc.attendancesystem.util.PictureManager;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by cwj on 2017/11/6.
 *
 * @author cwj
 */

public class PersonManageAdapter extends RecyclerView.Adapter<PersonManageAdapter.ViewHolder> {
    private List<PersonEntity> info, selected;
    private Context context;
    private LayoutInflater inflater;
    private String src;
    private PersonDialog dialog;
    private PopupWindow window;
    private TextView delete;
    private TextView all;
    private TextView edit;
    private PersonManageFragment fragment;
    private boolean editStatus, selectAll;
    private int page;
    private PageHelper<PersonEntity> personEntityPageHelper;
    PersonContract.Presenter presenter;

    public PersonManageAdapter(PersonContract.Presenter presenter, Context context, PersonDialog dialog, PersonManageFragment fragment, PageHelper<PersonEntity> personEntityPageHelper) {
        this.presenter = presenter;
        this.context = context;
        this.dialog = dialog;
        this.fragment = fragment;
        this.personEntityPageHelper = personEntityPageHelper;
        selected = new ArrayList<>();
        inflater = LayoutInflater.from(context);
    }

    public void addData(List<PersonEntity> info) {
        this.info = info;
        notifyDataSetChanged();
    }


    public boolean getEditStatus() {
        return editStatus;
    }

    public void setEditStatus(boolean editStatus) {
        this.editStatus = editStatus;
        selected.clear();
    }

    public void selectAll(int page, boolean select) {
        if (select) {
            this.page = page;
            if (!selectAll) {
                selectAll = true;
                selected.addAll(info);
                notifyDataSetChanged();
            } else {
                selectAll = false;
                selected.clear();
                notifyDataSetChanged();
            }
        }else {
            selectAll = false;
            selected.clear();
        }


    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        return new ViewHolder(inflater.inflate(R.layout.item_manage_recy, parent, false));
    }

    PersonEntity longPersonEntity;
    @Override
    public void onBindViewHolder(final ViewHolder holder,  int position) {
        final PersonEntity personEntity = info.get(position);
        byte[] image = info.get(position).getImage();
        if (image == null) {
            PictureManager.getInstance().loadCirclePic(context, R.drawable.ic_user_img, holder.mIvManageUserHead, true);

        } else {
            PictureManager.getInstance().loadCirclePic(context, image, holder.mIvManageUserHead, true);
        }

        String code = personEntity.getCode();
        if (!TextUtils.isEmpty(code)) {
            holder.mTvManageUserCode.setText(code);
        }
        String name = personEntity.getName();
        if (!TextUtils.isEmpty(name)) {
            holder.mTvManageUserName.setText(name);
        }
        String position2 = personEntity.getPosition();
        if (!TextUtils.isEmpty(position2)) {
            holder.mTvManageUserPosition.setText(position2);
        }
        String sex = personEntity.getSex();
        if (!TextUtils.isEmpty(sex)) {
            holder.mTvManageUserSex.setText(sex);
        }
        String card = personEntity.getCard();
        if (!TextUtils.isEmpty(card)) {
            holder.mTvManageUserCard.setText(card);
        }

        if (editStatus) {
            holder.mCbManageUserUser.setVisibility(View.VISIBLE);
            if (selectAll) {
                if (page == personEntityPageHelper.getCurrentPage()) {
                    holder.mCbManageUserUser.setChecked(true);
                } else {
                    selected.clear();
                    selectAll = false;
                    holder.mCbManageUserUser.setChecked(false);
                }
            } else {
                holder.mCbManageUserUser.setChecked(false);
            }
        } else {
            holder.mCbManageUserUser.setVisibility(View.INVISIBLE);
        }

        holder.mLlManageUserPosition.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (editStatus) {
                    boolean checked = holder.mCbManageUserUser.isChecked();
                    if (checked) {
                        holder.mCbManageUserUser.setChecked(false);
                        selected.remove(personEntity);
                    } else {
                        holder.mCbManageUserUser.setChecked(true);
                        selected.add(personEntity);
                    }
                }
            }
        });

        if (position % 2 == 0) {
            holder.mLlManageUserPosition.setBackground(context.getResources().getDrawable(R.drawable.bg_manage_list));
        } else {
            holder.mLlManageUserPosition.setBackground(context.getResources().getDrawable(R.drawable.bg_manage_list2));
        }

        holder.mEdit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (!editStatus) {
                    longPersonEntity = personEntity;
                    showDialog(personEntity);
                }
            }
        });
    }

    @Override
    public int getItemCount() {
        return info == null ? 0 : info.size();
    }

    public List<PersonEntity> getSelected() {
        return selected;
    }


    class ViewHolder extends RecyclerView.ViewHolder {

        ImageView mIvManageUserHead;
        TextView mTvManageUserCode;
        TextView mTvManageUserName;
        TextView mTvManageUserSex;
        TextView mTvManageUserCard;
        TextView mTvManageUserPosition;
        LinearLayout mLlManageUserPosition,mEdit;
        CheckBox mCbManageUserUser;

        ViewHolder(View itemView) {
            super(itemView);
            setIsRecyclable(false);
            mIvManageUserHead = itemView.findViewById(R.id.iv_manage_user_head);
            mCbManageUserUser = itemView.findViewById(R.id.cb_manage_user);
            mTvManageUserCode = itemView.findViewById(R.id.tv_manage_user_code);
            mTvManageUserCard = itemView.findViewById(R.id.tv_manage_user_card);
            mTvManageUserName = itemView.findViewById(R.id.tv_manage_user_name);
            mTvManageUserSex = itemView.findViewById(R.id.tv_manage_user_sex);
            mTvManageUserPosition = itemView.findViewById(R.id.tv_manage_user_position);
            mLlManageUserPosition = itemView.findViewById(R.id.ll_manage_person);
            mEdit = itemView.findViewById(R.id.ll_item_manage_edit);
        }
    }


    private AlertDialog dialogItem;
    private CustomDialog customdialog;
    private static final String TAG = "PersonManageAdapter";
    private void showDialog(final PersonEntity entity) {
        Log.i(TAG, "showDialog: "+entity.getId());
        if (dialogItem == null) {
            View view = LayoutInflater.from(context).inflate(R.layout.manager_alter_user, null, false);


            edit = view.findViewById(R.id.tv_manage_alter_edit);
            edit.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    dialogItem.dismiss();
                    dialog.setPersonEntity(longPersonEntity);
                    dialog.show();
                }
            });

            delete = view.findViewById(R.id.tv_manage_alter_delete);
            delete.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    dialogItem.dismiss();

                    if (customdialog == null) {
                        View view = LayoutInflater.from(context).inflate(R.layout.dialog_delete_person, null);

                        customdialog = new CustomDialog.Builder(context)
                                .setContentView(view)
                                .setNegativeButton("取消", new DialogInterface.OnClickListener() {
                                    @Override
                                    public void onClick(DialogInterface dialog, int which) {
                                        customdialog.dismiss();
                                    }
                                })
                                .setPositiveButton("确定", new DialogInterface.OnClickListener() {
                                    @Override
                                    public void onClick(DialogInterface dialog, int which) {
                                        presenter.deletePerson(longPersonEntity);
                                        customdialog.dismiss();
                                    }
                                }).build();
                    } else {
                        customdialog.show();
                    }

                }
            });

           /* all = view.findViewById(R.id.tv_manage_alter_all);
            all.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    editStatus = true;
                    dialogItem.dismiss();
                    fragment.alterAll();
                }
            });*/

            AlertDialog.Builder builder = new AlertDialog.Builder(context);
            builder.setView(view);
            dialogItem = builder.create();
        }
        dialogItem.show();
    }


}
